﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Coronox.SkyShell;
using System.Text.RegularExpressions;
using SkyShell.ViewModel;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using System.Threading.Tasks;


namespace SkyShell
{
    /// <summary>
    /// Interaction logic for ShareContent.xaml
    /// </summary>
    public partial class ShareContent : Page
    {
        public ShareContent()
        {
            InitializeComponent();
            ShareContentViewModel viewModel = this.DataContext as ShareContentViewModel;
            viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(viewModel_PropertyChanged);

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            ShareContentViewModel viewModel = this.DataContext as ShareContentViewModel;

            //Messenger.Default.Register<string>(this, (m) => DispatcherHelper.CheckBeginInvokeOnUI(() => 
            //{
            //    if (m == "Open Login Window")
            //    {
            //        //LoginWebBrowser.Navigate(SkyDriveAction.SignInUrl);
            //    }
            //}));

            //Messenger.Default.Register<Tuple<string, bool>>(this, (t) => DispatcherHelper.CheckBeginInvokeOnUI(() =>
            //{
            //    if (t.Item1 == "IsLoginBrowserVisible")
            //    {
            //       // this.LoginWebBrowser.Visibility = t.Item2 ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            //    }
            //}));

            if (viewModel != null)
            {
                viewModel.Load();
            }
        }

        void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ReadonlySharedLink")
            {
                Task t = new Task(() =>
                    {
                        System.Threading.Thread.Sleep(100);
                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                textBoxReadonlyLink.SelectAll();
                                textBoxReadonlyLink.Focus();
                            });
                    });
                t.Start();
            }
            if (e.PropertyName == "EditableSharedLink")
            {
                Task t = new Task(() =>
               {
                   System.Threading.Thread.Sleep(100);
                   DispatcherHelper.CheckBeginInvokeOnUI(() =>
                      {
                          textBoxEditableLink.SelectAll();
                          textBoxEditableLink.Focus();
                      });
               });
                t.Start();
            }
        }

    }
}
